package com.uhylab.uhuvinetest.activity;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.adapter.UHDisHistory_ListAdapter;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.dao.DaoSingleton;
import com.uhylab.uhuvinetest.dao.UserDisHistoryEntity;
import com.uhylab.uhuvinetest.dao.UserDisHistoryEntityDao;
import com.uhylab.uhuvinetest.view.OnScrollViewListView;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称：Uhylab
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/5/31 14:12
 * 修改人：xyb
 * 修改时间：16/5/31 14:12
 * 修改备注：
 */
public class UHDiseaseHistoryActivity extends UHBaseActivity implements AdapterView.OnItemLongClickListener {


    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.list_dis)
    OnScrollViewListView mListDis;
    @BindView(R.id.dis_four_re)
    AutoRelativeLayout mDisFourRe;
    @BindView(R.id.dis_history_editText)
    EditText mDisHistoryEditText;

    private ArrayList<String> mStrings;
    private UHDisHistory_ListAdapter mListAdapter;
    private UserDisHistoryEntityDao mUserDisHistoryEntityDao;

    /**
     * 绑定布局
     *
     * @return int
     */
    @Override
    public int bindLayout() {
        return R.layout.activity_uhdisease_history;
    }

    /**
     * 初始化布局
     *
     * @param view
     */
    @Override
    public void initView(View view) {
        ButterKnife.bind(this);
    }

    /**
     * 业务处理操作（onCreate方法中调用）
     *
     * @param mContext 当前Activity对象
     */
    @Override
    public void doBusiness(Context mContext) {
        mStrings = new ArrayList<>();
        mUserDisHistoryEntityDao = DaoSingleton.getInstance().getUserDisHistoryEntityDao();
        List<UserDisHistoryEntity> userDisHistoryEntities = mUserDisHistoryEntityDao.loadAll();
        if (userDisHistoryEntities != null && userDisHistoryEntities.size() > 0) {
            for (UserDisHistoryEntity userDisHistoryEntity :
                    userDisHistoryEntities) {
                mStrings.add(userDisHistoryEntity.getHistory());
            }
        }
        mListAdapter = new UHDisHistory_ListAdapter(this, mStrings, mUserDisHistoryEntityDao);
        mListDis.setFocusable(false);
        mListDis.setAdapter(mListAdapter);
        mListDis.setOnItemLongClickListener(this);
    }

    /**
     * 暂停恢复刷新相关操作（onResume方法中调用）
     */
    @Override
    public void resume() {

    }

    /**
     * 销毁、释放资源相关操作（onDestroy方法中调用）
     */
    @Override
    public void destroy() {

    }


    @OnClick({R.id.back, R.id.dis_four_re})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.dis_four_re:
                if (mDisHistoryEditText.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入病史,再添加", Toast.LENGTH_SHORT).show();
                    return;
                }
                mListAdapter.addString(mDisHistoryEditText.getText().toString());
                mListAdapter.notifyDataSetChanged();
                mDisHistoryEditText.setText("");
                break;
        }
    }


    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        String itemAtPosition = (String) parent.getItemAtPosition(position);
        mListAdapter.removeString(itemAtPosition);
        mListAdapter.notifyDataSetChanged();
        return false;
    }
}
